﻿using System.Drawing;
using System.Windows.Forms;
using DirectMobile.Controls;
using Microsoft.WindowsMobile.DirectX;
using Microsoft.WindowsMobile.DirectX.Direct3D;

namespace DirectMobile
{
    public sealed class HostForm : Form
    {
        protected internal Device Device;
        private int _frames;
        private string _frameratemsg = string.Empty;
        private int _lastTickCount;
        
        public Frame ActiveFrame { get; set; }

        internal bool InitializeGraphics()
        {
            try
            {
                var presentParams = new PresentParameters { Windowed = true, SwapEffect = SwapEffect.Discard };
                Device = new Device(0, DeviceType.Default, this, CreateFlags.None, presentParams);
            }
            catch (DirectXException)
            {
                return false;
            }
            return true;
        }

        /// <summary>
        /// Renders the Direct3D Scence
        /// </summary>
        private void Render()
        {
            if (Device == null)
                return;

            Device.Clear(ClearFlags.Target, Color.Black, 1.0f, 0);
            Device.BeginScene();
            Device.DrawPrimitives(PrimitiveType.TriangleList,0,1);
            Device.EndScene();
            Device.Present();
        }

        // Called to repaint the window
        protected override void OnPaint(PaintEventArgs e)
        {
            Render();
            Invalidate();
        }

        // Called to repaint the window background. No actions here to avoid rendering ontop of DirectX
        protected override void OnPaintBackground(PaintEventArgs e) { }
    }
}
